package DAO;

import service.Service;
import service.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DAO {
    private String url;
    private String driver;
    private String username;
    private String password;
    private Service service;

    private Connection getConnection(){
        Connection conn=null;
        try{
            Class.forName(this.getDriver());
            conn= DriverManager.getConnection(this.getUrl(),this.getUsername(),this.getPassword());
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    public  int saveDAO(){
        int i=0;
        try{
            Connection conn=this.getConnection();
            String sql="insert into "+this.getService().getDatabase()+"(name,password) values(?,?)";
            System.out.println(sql);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,this.getService().save()[0]);
            ps.setString(2,this.getService().save()[1]);
            i=ps.executeUpdate();
            ps.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
    public int delDAO(){
        int i=0;
        try{
            Connection conn=this.getConnection();
            String sql="delete from "+this.getService().getDatabase()+" where id=?";
            System.out.println(sql);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,this.getService().delNum());
            i=ps.executeUpdate();
            ps.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
    public int editDAO(){
        int i=0;
        try{
            Connection conn=this.getConnection();
            String sql="update "+this.getService().getDatabase()+" set name=?,password=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ps.setString(1,this.getService().edit()[0]);
            ps.setString(2,this.getService().edit()[1]);
            ps.setInt(3,this.getService().editNum());
            i=ps.executeUpdate();
            ps.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
    public void queryDAO(){
        try{
            Connection conn=this.getConnection();
            String sql="select id,name,password from "+this.getService().getDatabase()+" where name like ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ps.setString(1,"%"+this.getService().query()+"%");
            ResultSet rs =ps.executeQuery();
            if(!(null==rs||"".equals(rs))){
                System.out.println("id===username===password");
                while(rs.next()){
                    System.out.println(rs.getInt("id")+"==="+rs.getString("name")+"==="+rs.getString("password"));
                }
            }
            ps.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void queryAllDAO(){
        try{
            Connection conn=this.getConnection();
            String sql="select id,name,password from "+this.getService().getDatabase();
            PreparedStatement ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ResultSet rs =ps.executeQuery();
            if(!(null==rs||"".equals(rs))){
                System.out.println("id===username===password");
                while(rs.next()){
                    System.out.println(rs.getInt("id")+"==="+rs.getString("name")+"==="+rs.getString("password"));
                }
            }
            ps.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }
}
